import Request from '@/js_sdk/luch-request/index.js'
import {
	BASE_URL
} from '@/common/config.js'
import {
	_getStorage
} from '@/common/storage.js'
import {store} from 'vuex'


const request = new Request({
	baseURL: BASE_URL
})

request.interceptors.request.use(config => {
	let token = _getStorage('token') ||'';
	let type = _getStorage('type') ||'';
	let AuthorizationKey = type==1?'Merchant-Session-Id':'Staff-Session-Id'
	if (token) {
		config.header = {
			...config.header,
			[AuthorizationKey]: token,
			"VERSION":2
		}
	}
	return config;
}, config => {
	return Promise.reject(config);
})

request.interceptors.response.use(response => {
	if (response.statusCode == 200 || response.statusCode == 201 || response.statusCode == 202 || response
		.statusCode ==
		203 || response.statusCode == 204 || response.statusCode == 205 || response.statusCode == 206) {
		return response.data || response.tempFilePath;
	}

	return Promise.reject(response && response.data && response.data.error ?
		response.data.error : response && response.data ? response.data : response.msg);
}, response => {
	let {statusCode} = response;
	if(statusCode==451){//登录失效
		uni.reLaunch({
			url:"/pages/Common/Login/index"
		})
	}else{
		let message = response && response.data ? response.data.msg:'请求错误'
		uni.showToast({
			title: message,
			icon: 'none'
		})
	}
	return Promise.reject(response && response.data && response.data.error ?
		response.data.error : response && response.data ? response.data : response.msg);
})

export {
	request
}
